<?php

class Core_Backend_Component_Promotion_View_Ad_Create extends Core_Backend_Share_View_Form {

    protected function initialize() {
        parent::initialize();
        $this->setTemplatePath('edit');
    }

    protected function buildToolbar($toolbar) {
        $group = $toolbar->createGroup('back');
        $group->addLink($this->getHistoryUrl('promotion/ad/index'), $this->__('share.button.back'));
        if($this->getIdentity()->isAllowAction('promotion_ad_edit')){
	        $group = $toolbar->createGroup('save');
	        $group->addButton($this->__('share.button.submit'))
	                ->bindEvent('click', "Kdf.Ui.getForm('" . $this->getForm()->getId() . "').submit('" . $this->getUrl(array('status'=>'pending'), 'save') . "')");
	        $group->addButton($this->__('share.button.save'))
	                ->bindEvent('click', "Kdf.Ui.getForm('" . $this->getForm()->getId() . "').submit('" . $this->getUrl(array('status'=>'draft'), 'save') . "')");
        }
        parent::buildToolBar($toolbar);
    }

    public function buildForm($form) {
        $entity = $this->getModel(NULL);
        $form->setAction($this->getUrl())
                ->setId('ad_form')
                ->addHiddenField('id', 'ad[id]');
        $form->addTextboxField($this->__('share.label.title'), 'title', 'ad[title]', TRUE)
                ->addMultiRule('required');
        $form->addTextboxField($this->__('share.label.url'), 'url', 'ad[url]', TRUE)
                ->addUrlRule();
        $form->addSelectField($this->__('promotion.ad.type'), 'type', 'ad[type]',TRUE)
                ->setOption('--', '0')
                ->setOption($this->__('promotion.ad.word'), 'introduction')
                ->setOption($this->__('promotion.ad.picture'), 'picture')
                ->setOption($this->__('promotion.ad.flash'), 'flash')
                ->setOption($this->__('promotion.ad.animation'), 'animation');
        $form->addTextboxField($this->__('promotion.ad.word'), 'introduction', 'ad[introduction]')->getField()->setHidden(TRUE);
        $form->addFileBrowserField($this->__('share.label.flash'), 'flash', 'ad[flash]')
                ->setPath('pub/media/ad/flash')
                ->setFileType('flash')
                ->setCallback('selectAdflash')
                ->setHidden(TRUE)
                ->getField()
                ->setHidden(TRUE);
        $form->addFileBrowserField($this->__('share.label.picture'), 'picture', 'ad[picture]')
                ->setPath('pub/media/ad/picture')
                ->setFileType('image')
                ->setCallback('selectAdPictute')
                ->setHidden(TRUE)
                ->getField()
                ->setHidden(TRUE);
        $form->addSelectField($this->__('share.ad.effect'), 'animation', 'ad[effect]')
                ->setOption('--', '')
                ->setOption($this->__('promotion.ad.rotate'), 'rotate')
                ->setOption($this->__('promotion.ad.rollleft'), 'rollleft')
                ->setOption($this->__('promotion.ad.rollright'), 'rollright')
                ->setOption($this->__('promotion.ad.rollup'), 'rollup')
                ->setOption($this->__('promotion.ad.rolldown'), 'rolldown')
                ->getField()
                ->setHidden(TRUE);
        $form->addDatetimeField($this->__('share.label.start_time'), 'start_time', 'ad[start_time]')
                ->setCurrentDate()->setFormat('Y-m-d')->addRequiredRule();
        $form->addDatetimeField($this->__('share.label.end_time'), 'end_time', 'ad[end_time]')
                ->setFormat('Y-m-d')->addRequiredRule();
        $form->addSelectField($this->__('share.label.position'), 'position', 'ad[position]',TRUE)
                ->bindData($this->getPositions(), 'name', 'code');
                
        if (!is_null($entity)) {
            $form->bindData($entity->getAllData());
        }
        parent::buildForm($form);
    }

}

